$(function () {
    $("#sort1").click(function () {
        findMvBySort(1);
    });
    $("#sort2").click(function () {
        findMvBySort(2);
    });
    $("#sort3").click(function () {
        findMvBySort(3);
    });
    $("#sort4").click(function () {
        findMvBySort(4);
    });
    $("#sort5").click(function () {
        findMvBySort(5);
    });
    /**
     * 加载音乐
     */
    addSongBySort(1, 1);

    addsong(1);

    //切换图片
    var timer;
    changePic.onmouseover = function () {
        window.clearTimeout(timer);
    };
    changePic.onmouseout = function () {
        showPic();
    };

    setInterval(showPic, 2000);
});

function csong(songid) {
    window.location.href = "../html/player.html?songid=" + songid;
}

function findMvBySort(nation) {
    addSongBySort(1, nation);
}

function addSongBySort(pageNum, nation) {
    $.get(path + "/song/song/list", {"pageNum": pageNum, "pageSize": 10, "nation": nation}, function (result) {
        if (result.code === 200) {
            $("#mv_hotSort").empty();
            var list = result.list;
            var index = 1;
            $(list).each(function () {
                var div = '<div onclick="csong(' + this.id + ')" style="cursor:pointer;"><a href="/sites/player.html?songid=' + this.id + '"><span>&nbsp;&nbsp;' + index + '&nbsp;&nbsp;&nbsp;&nbsp;' + this.songName + '</span></a></div>';
                $("#mv_hotSort").append(div);
                $("#mv_hotSort div:last").data("song", this);
                index++;
            });
        } else {
            alert(result.msg);
        }
    });
}

/**
 * 加载音乐
 */
function addsong(pageNum) {
    $.get(path + "/song/song/page-list", {"pageNum": pageNum, "pageSize": 10}, function (result) {
        if (result.code === 200) {
            $("#mv_Pics").empty();
            $("#pageChange").empty();
            $("#pageChange").append('<span class="pageSpan">页数:&nbsp;</span>');
            var list = result.list;
            var index = 1;
            $(list).each(function () {
                var div = '<div class="songPics"><a href="/sites/player.html?songid=' + this.id + '"><img src="' + this.headImg + '"/></a> &nbsp;&nbsp;&nbsp;' + this.songName + '</div>';
                $("#mv_Pics").append(div);
                $("#mv_Pics div:last").data("song", this);
                $("#mv_Pics div:last").addClass("select_pic" + index);
                index++;
            });

            var page = result.page;
            for (var i = 1; i <= page.pageCount; i++) {
                var a = '<span class="pageSpan"><a href="javascript:addsong(' + i + ')">' + i + '&nbsp;</a></span>&nbsp;';
                $("#pageChange").append(a);
            }
            //总数
            let size = index - 1;
            $("#pageChange").data("pageSize", size);
            /**
             * 加载浮动图片
             */
            addfloatpic(pageNum, size);
        } else {
            alert(result.msg);
        }
    });
}

/**
 *
 * @param pageNum
 * @param pageSize
 */
function addfloatpic(pageNum, pageSize) {
    if (pageNum > pageSize) {
        pageNum = 1;
    }
    let song = $(".select_pic" + pageNum).data("song");
    var a = '<a href="/sites/player.html?songid=' + song.id + '"><img src="' + song.headImg + '" alt=""/></a>';
    $("#changePic").html(a);
    pageNum++;
    $("#changePic").data("pageNum", pageNum);


}

function showPic() {
    var pageNum = $("#changePic").data("pageNum");
    var pageSize = $("#pageChange").data("pageSize");
    addfloatpic(pageNum, pageSize);
//	setInterval(showPic,1000);
}

